package com.ruoyi.web.controller.drugSystem.drugInfo;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.drugSystem.domain.SysDrugInfo;
import com.ruoyi.drugSystem.service.ISysDrugBrandService;
import com.ruoyi.drugSystem.service.ISysDrugClassService;
import com.ruoyi.drugSystem.service.ISysDrugInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author 努力的派大星
 * @version 1.0
 */
@Api("药品分类信息管理")
@RestController
@RequestMapping("/drugInfo/drugDetailedInfo")
public class SysDrugInfoController extends BaseController{
    @Autowired
    private ISysDrugInfoService drugInfoService;
    @Autowired
    private ISysDrugClassService drugClassService;
    @Autowired
    private ISysDrugBrandService drugBrandService;

    /**
     * 获取药物列表
     */
    @PreAuthorize("@ss.hasPermi('drugInfo:drugDetailedInfo:list')")
    @GetMapping("/list")
    @ApiOperation("药物列表")
    public TableDataInfo list(SysDrugInfo drugInfo)
    {
        startPage();
        List<SysDrugInfo> list = drugInfoService.selectDrugInfoList(drugInfo);
        return getDataTable(list);
    }

    @Log(title = "药物管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('drugInfo:drugDetailedInfo:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDrugInfo drugInfo)
    {
        List<SysDrugInfo> list = drugInfoService.selectDrugInfoList(drugInfo);
        ExcelUtil<SysDrugInfo> util = new ExcelUtil<SysDrugInfo>(SysDrugInfo.class);
        util.exportExcel(response, list, "药物数据");
    }
    @ApiOperation("class")
    @GetMapping("/getClass")
    public AjaxResult getDrugClass()
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("classes",drugClassService.selectDrugClassAll());
        ajax.put("brands", drugBrandService.selectSysDrugBrandAll());
        return ajax;
    }
    /**
     * 根据药物编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('drugInfo:drugDetailedInfo:query')")
    @GetMapping(value = "/{drugInfoId}")
    public AjaxResult getInfo(@PathVariable Long drugInfoId)
    {
        return success(drugInfoService.selectDrugInfoById(drugInfoId));
    }

    /**
     * 新增药物
     */
    @ApiOperation("增加药物")
    @PreAuthorize("@ss.hasPermi('drugInfo:drugDetailedInfo:add')")
    @Log(title = "药物管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDrugInfo drugInfo)
    {
        if (!drugInfoService.checkDrugInfoNameUnique(drugInfo))
        {
            return error("新增药物'" + drugInfo.getDrugdetailedinfoName()+ "'失败，药物名称已存在");
        }
        else if (!drugInfoService.checkDrugInfoCodeUnique(drugInfo))
        {
            return error("新增药物'" + drugInfo.getDrugdetailedinfoCode() + "'失败，药物编码已存在");
        }
        drugInfo.setCreateBy(getUsername());
        return toAjax(drugInfoService.insertDrugInfo(drugInfo));
    }

    /**
     * 修改药物
     */
    @PreAuthorize("@ss.hasPermi('drugInfo:drugDetailedInfo:edit')")
    @Log(title = "药物管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改药物")
    public AjaxResult edit(@Validated @RequestBody SysDrugInfo drugInfo)
    {
        if (!drugInfoService.checkDrugInfoNameUnique(drugInfo))
        {
            return error("修改药物'" + drugInfo.getDrugdetailedinfoName() + "'失败，药物名称已存在");
        }
        else if (!drugInfoService.checkDrugInfoCodeUnique(drugInfo))
        {
            return error("修改药物'" + drugInfo.getDrugdetailedinfoName() + "'失败，药物编码已存在");
        }
        drugInfo.setUpdateBy(getUsername());
        return toAjax(drugInfoService.updateDrugInfo(drugInfo));
    }

    /**
     * 删除药物
     */
    @ApiOperation("删除药品")
    @PreAuthorize("@ss.hasPermi('drugInfo:drugDetailedInfo:remove')")
    @Log(title = "药物管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugInfoIds}")
    public AjaxResult remove(@PathVariable Long[] drugInfoIds)
    {
        return toAjax(drugInfoService.deleteDrugInfoByIds(drugInfoIds));
    }

    /**
     * 获取药物选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDrugInfo> drugInfos = drugInfoService.selectDrugInfoAll();
        return success(drugInfos);
    }
}

